<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import wsjzglService from '@/api/wsjzgl'
import { getFldm } from '@/utils/get-fldm'
import type { FormInstance, FormRules } from 'element-plus'
import { mlxxMainStore } from '@/store/mlxx'
const route = useRoute()
const emit = defineEmits(['refresh'])
const mlxxStrore = mlxxMainStore()

const ruleFormRef = ref()
// 入参

/* 弹框状态 */
const dialogVisible = ref(false)
/*
 * 打开弹框
 */
const openDialog = async () => {
  await getAjlx()
  dialogVisible.value = true
}

/*
 * 保存（成功抛出刷新）
 */
const save = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      const params = {
        ajlxdm: ruleForm.value.ajlxdm,
        ajlxmc: ruleForm.value.ajlxmc,
        ajmc: mlxxStrore.daxqData?.ajmc as string,
        ajbh: mlxxStrore.daxqData?.ajbh as string,
      }
      wsjzglService
        .addAjxx(params)
        .then(() => {
          ElMessage({ type: 'success', message: '保存成功' })
          emit('refresh')
          closeDialog()
        })
        .catch((err) => {
          !err.isError && ElMessage({ type: 'warning', message: err.message })
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const ruleForm = ref({
  ajlxdm: '',
  ajlxmc: '',
})
/* 表单校验 */
const rules = ref<FormRules>({
  ajlxdm: [
    {
      required: true,
      message: '请选择',
      trigger: 'change',
    },
  ],
})

/*
 * 获取案卷类别
 */
const ajlxList = ref<any[]>([])
const getAjlx = async () => {
  let newArr = await getFldm('gd022')
  ajlxList.value = newArr.filter((item: any) => item.dm !== 'gd0220003')
}

const ajlxChange = (value: number) => {
  ruleForm.value.ajlxdm = ajlxList.value[value].dm
  ruleForm.value.ajlxmc = ajlxList.value[value].mc
}

/*
 * 关闭弹框
 */
const closeDialog = () => {
  dialogVisible.value = false
  ruleForm.value = {
    ajlxdm: '',
    ajlxmc: '',
  }
  nextTick(() => {
    ruleFormRef.value?.resetFields()
  })
}

defineExpose({
  openDialog,
})
</script>
<template>
  <!-- 添加卷弹窗 -->
  <el-dialog
    v-model="dialogVisible"
    title="添加卷"
    width="50%"
    :close-on-click-modal="false"
    :before-close="closeDialog"
  >
    <el-form ref="ruleFormRef" label-width="auto" :model="ruleForm" :rules="rules">
      <el-form-item label="正副案卷" required prop="ajlxdm">
        <el-select v-model="ruleForm.ajlxmc" placeholder="请选择" @change="ajlxChange">
          <el-option
            v-for="(item, index) in ajlxList"
            :key="item.dm"
            :label="item.mc"
            :value="index"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="save(ruleFormRef)" load>保 存</el-button>
        <el-button @click="closeDialog">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>
